import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/')
  },
  {
    path: '/',
    component: () => import('@/views/home/'),
    children: [
      {
        path: '/userstatistics',
        name: 'userstatistics',
        component: () => import('@/views/userstatistics/')
      },
      {
        path: '/videostatistics',
        name: 'videostatistics',
        component: () => import('@/views/videostatistics/')
      },
      {
        path: '/video',
        name: 'video',
        component: () => import('@/views/video/')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('@/views/user/')
      },
      {
        path: '/videolable',
        name: 'videolable',
        component: () => import('@/views/videoLabel/')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})

export default router
